package com.yej.aiPolice.ui.fragment

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.yej.aiPolice.R
import com.yej.aiPolice.model.beans.User
import com.yej.aiPolice.ui.activity.AddFaceActivity
import com.yej.aiPolice.ui.activity.LoginActivity
import com.yej.aiPolice.ui.activity.MainActivity
import com.yej.aiPolice.ui.activity.web.AboutActivity
import com.yej.aiPolice.ui.activity.web.InfoActivity
import com.yej.aiPolice.ui.activity.web.PasswordActivity
import com.yej.aiPolice.util.AiPoliceAPP
import kotlinx.android.synthetic.main.fragment_user.*
import org.jetbrains.anko.find
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.find as find1


class UserFragment : Fragment() {

    var changePw = null
    var outButton = null
    var aboutButon = null
    //创造view才能看到Fragment效果
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val userView = View.inflate(activity, R.layout.fragment_user, null)
        val ivlogin = userView.find1<ImageView>(R.id.login)
        val ll_userinfo = userView.find<LinearLayout>(R.id.ll_userinfo)
        val username = userView.find<TextView>(R.id.username)
        val phone=userView.find<TextView>(R.id.phone)
        val changeInfo = userView.find<FrameLayout>(R.id.fl_change_info)
        ivlogin.setOnClickListener {
            val intent : Intent = Intent(activity, LoginActivity::class.java)
            startActivity(intent)
            Log.e("user","跳转成功")
        }
        return userView
    }

    override fun onStart() {
        super.onStart()
        //展示登陆成功后效果
        val user = AiPoliceAPP.sUser;
        login.visibility = View.VISIBLE
        if(AiPoliceAPP.sUser.id == -1){
            //未登录
            ll_userinfo.visibility = View.GONE

        }else{

            login.visibility = View.GONE
            ll_userinfo.visibility = View.VISIBLE
            username.text = "欢迎您，${user.username}"
            phone.text = "欢迎您，${user.tel}"
            fl_change_password.setOnClickListener {
                context?.startActivity<PasswordActivity>()
            }
            fl_about.setOnClickListener {
                context?.startActivity<AboutActivity>()
            }

            fl_change_info.setOnClickListener {
                context?.startActivity<InfoActivity>()
            }

            fl_out.setOnClickListener {
                AiPoliceAPP.sUser = User()
                AiPoliceAPP.sUser.id = -1
                context?.startActivity<MainActivity>()
            }

            fl_upload_face.setOnClickListener {
                context?.startActivity<AddFaceActivity>()
            }



        }
    }

}